package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.addProviderBean;
import com.ymatou.iapi.godmch.parameter.addServiceRelationBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.addProviderCall;
import com.ymatou.iapi.godmch.service.addServiceRelationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.serviceRelation;
import com.ymttest.database.sqlwapper.godmch.serviceRelationWapper;
/**
*为渠道商指定服务商
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_addServiceRelation{
	private static addServiceRelationBean bean;
	private static addServiceRelationCall call;
	
	serviceRelationWapper relationWapper=new serviceRelationWapper();
	
	addProviderBean addbean=new addProviderBean();
	addProviderCall addcall=new   addProviderCall();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("为渠道商指定服务商");
	}

	@Before
	public void caseUp() {
		bean=new addServiceRelationBean();
		call=new addServiceRelationCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void check_verify() throws JSONException, InterruptedException
	{
		//验证
		serviceRelation serviceRelation=relationWapper.selectByPrimaryKey(bean.getProviderId(), bean.getMchId(), bean.getServiceType().get(0));
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
		Logger.verifyEquals(true, call.getSuccess(), "验证success");
		
		Logger.verifyEquals(0, serviceRelation.getState(), "验证serviceRelation表state=0");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_001() {
		Logger.start(true,"正例-为渠道商指定服务商-商户是渠道+代理");
		try {

			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			createmchbean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CAN");//设为国内，变为纯代理商
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			//创建服务商
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			addbean.setProviderId(userId_agent);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();	
			
			bean.setProviderId(userId_agent);
			bean.setMchId(userId_channl);
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();	
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_001_1() {
		Logger.start(true,"正例-为渠道商指定服务商-商户是纯渠道");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			int providerId=pBean.getMchId();
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			GodmchManageCallService.createmchCallForChannel(mchBean);
			bean.setProviderId(providerId);
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();		
			check_verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_001_2() {
		Logger.start(true,"正例-为渠道商指定服务商-商户是VIP渠道");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			int providerId=pBean.getMchId();
			GodmchManageCallService.SetVip(providerId);
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			GodmchManageCallService.createmchCallForChannel(mchBean);
			bean.setProviderId(providerId);
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();		
			check_verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_001_4() {
		Logger.start(true,"正例-为渠道商指定服务商-渠道商的国家不在服务商列表中");
		try {
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			int providerId=pBean.getMchId();
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("NZL");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			bean.setProviderId(providerId);
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_002() {
		Logger.start(true,"反例-为渠道商指定服务商-商户是纯代理，返回失败");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent2=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			createmchbean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			//代理+渠道
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CAN");//设为国内，变为纯代理商
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			//纯代理
			List<createmchBean> createmchList2=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent2);
			createmchList2.get(0).setCountry("CHN");//设为国内，变为纯代理商
			GodmchManageCallService.createmchCallForAgent(createmchList2.toArray(new
					createmchBean[createmchList2.size()]));
			
			//创建服务商
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			countryList.add("CHN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			addbean.setProviderId(userId_agent);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();	
			
			bean.setProviderId(userId_agent);
			bean.setMchId(userId_agent2);
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_003() {
		Logger.start(true,"反例-为渠道商指定服务商-服务类型serviceType不存在");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			int providerId=pBean.getMchId();
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(11);
			
			GodmchManageCallService.createmchCallForChannel(mchBean);
			bean.setProviderId(providerId);
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_004() {
		Logger.start(true,"反例-为渠道商指定服务商-服务商id不存在");
		try {
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			//服务商
			//int providerId=GodmchManageCallService.InitAddProvider();
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			bean.setProviderId(99);
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("服务商不存在"), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_005() {
		Logger.start(true,"反例-为渠道商指定服务商-服务商id为空");
		try {
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			//服务商
			//int providerId=GodmchManageCallService.InitAddProvider();
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
		//	bean.setProviderId(99);
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
		//	Logger.verifyEquals(true, call.getMessage().contains("服务商不存在"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_006() {
		Logger.start(true,"反例-为渠道商指定服务商-商户id不存在");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			int providerId=pBean.getMchId();
//			//渠道
//			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
//			mchBean.setCountry("CAN");
//			GodmchManageCallService.createmchCallForChannel(mchBean);
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			bean.setProviderId(providerId);
			bean.setMchId(99);
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");	
			Logger.verifyEquals(true, call.getMessage().contains("渠道商不存在"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addServiceRelation_007() {
		Logger.start(true,"反例-为渠道商指定服务商-商户id为空");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			int providerId=pBean.getMchId();
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
//			//渠道
//			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
//			mchBean.setCountry("CAN");
//			GodmchManageCallService.createmchCallForChannel(mchBean);
			bean.setProviderId(providerId);
			bean.setMchId(99);
			bean.setServiceType(serviceType);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");		
			Logger.verifyEquals(true, call.getMessage().contains("渠道商不存在"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}